package wiki.hitime.stl.daq.manager;

import wiki.hitime.stl.daq.bean.bo.DataMsgParamBO;
import wiki.hitime.stl.daq.bean.db.DaqAcquisitionDataRecord;
import wiki.hitime.stl.daq.bean.db.DaqCheckRecord;
import wiki.hitime.stl.daq.bean.db.DaqExecuteRecord;
import wiki.hitime.stl.daq.mapper.DaqAcquisitionDataRecordMapper;
import wiki.hitime.stl.daq.mapper.DaqCheckRecordMapper;
import wiki.hitime.stl.daq.mapper.DaqExecuteRecordMapper;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhangdaoping
 * @create 2019-07-23 10:51
 */
@Service
public class DataMsgParamManager {

    private DaqCheckRecordMapper daqCheckRecordMapper;
    private DaqExecuteRecordMapper daqExecuteRecordMapper;
    private DaqAcquisitionDataRecordMapper daqAcquisitionDataRecordMapper;


    public DataMsgParamManager(DaqCheckRecordMapper daqCheckRecordMapper,
                               DaqExecuteRecordMapper daqExecuteRecordMapper,
                               DaqAcquisitionDataRecordMapper daqAcquisitionDataRecordMapper) {
        this.daqExecuteRecordMapper = daqExecuteRecordMapper;
        this.daqCheckRecordMapper = daqCheckRecordMapper;
        this.daqAcquisitionDataRecordMapper = daqAcquisitionDataRecordMapper;
    }

    /**
     * 加载消息参数
     *
     * @param checkRecordId 检查记录id
     * @return
     */
    public Map<String, String> loadMsgParams(Long checkRecordId) {
        DaqCheckRecord daqCheckRecord = daqCheckRecordMapper.selectByPrimaryKey(checkRecordId);
        DaqExecuteRecord daqExecuteRecord = daqExecuteRecordMapper.selectByPrimaryKey(daqCheckRecord.getExecuteRecordId());
        Example acqDataSelectExample = new Example(DaqAcquisitionDataRecord.class, true, true);
        acqDataSelectExample.createCriteria().andEqualTo("acquisitionRecordId", daqCheckRecord.getAcquisitionRecordId()).andEqualTo("isDelete", Boolean.FALSE);
        List<DaqAcquisitionDataRecord> acquisitionDataRecordList = daqAcquisitionDataRecordMapper.selectByExample(acqDataSelectExample);
        DataMsgParamBO dataMsgParamBO = DataMsgParamBO.builder()
                .executeDate(new DateTime(daqExecuteRecord.getCreated()).toString("yyyy-MM-dd HH:mm:ss"))
                .executeRuleName(daqExecuteRecord.getRuleName())
                .checkResult(daqCheckRecord.getCheckResult().toString())
                .checkSummary(daqCheckRecord.getCheckSummary())
                .build();
        Map<String, String> paramMap = new HashMap<>();
        if (acquisitionDataRecordList.size() > 0) {
            dataMsgParamBO.setFirstData(acquisitionDataRecordList.get(0).getDataA());
            dataMsgParamBO.setLastData(acquisitionDataRecordList.get(acquisitionDataRecordList.size() - 1).getDataA());
            for (int i = 0; i < acquisitionDataRecordList.size(); i++) {
                paramMap.put("value_" + i, acquisitionDataRecordList.get(i).getDataA());
            }
        }
        paramMap.putAll(dataMsgParamBO.loadParamMap());
        return paramMap;
    }

    /**
     * 获取可用消息键值
     *
     * @return
     */
    public List<String> listMsgParamKey() {
        List<String> paramList = new DataMsgParamBO().listMsgKey();
        paramList.add("value_0");
        return paramList;
    }

}
